package com.example.musicrecommend.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.musicrecommend.common.BaseResponse;
import com.example.musicrecommend.common.ErrorCode;
import com.example.musicrecommend.common.ResultUtils;
import com.example.musicrecommend.exception.BusinessException;
import com.example.musicrecommend.exception.ThrowUtils;
import com.example.musicrecommend.model.dto.comment.CommentAddRequest;
import com.example.musicrecommend.model.dto.comment.CommentQueryRequest;
import com.example.musicrecommend.model.entity.Comment;
import com.example.musicrecommend.model.vo.CommentVO;
import com.example.musicrecommend.service.CommentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;



@RestController
@RequestMapping("/comment")
@Slf4j
public class CommentController {

    @Resource
    private CommentService commentService;

    /**
     * 创建评论
     *
     * @param commentAddRequest
     * @param request
     * @return
     */
    @PostMapping("/add")
    public BaseResponse<Long> addComment(@RequestBody CommentAddRequest commentAddRequest, HttpServletRequest request) {
        if (commentAddRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Comment comment = new Comment();
        BeanUtils.copyProperties(commentAddRequest, comment);
        // 校验
        commentService.validComment(comment,true);
        boolean result = commentService.save(comment);
        ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR);
        return ResultUtils.success(comment.getId());
    }


    /**
     * 分页获取用户列表
     *
     * @param commentQueryRequest
     * @param request
     * @return
     */
    @GetMapping("/list/page")
    public BaseResponse<Page<CommentVO>> listCommentPageBySongId(CommentQueryRequest commentQueryRequest,
                                                           HttpServletRequest request) {
        long current = commentQueryRequest.getCurrent();
        long size = commentQueryRequest.getPageSize();


        Page<CommentVO> commentVOPage = commentService.selectCommentPageBySongId(new Page<>(current, size),
                commentQueryRequest.getSongId());

        return ResultUtils.success(commentVOPage);
    }
}
